import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/cartoon_media_info.dart';
import 'package:xvideos/hj_page/cartoon_business/acg_widget_item.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import 'cartoon_sectionlist_sub_logic.dart';

class CartoonSectionSubPage extends StatefulWidget {
  final int? sortType;
  final String? sectionID;

  const CartoonSectionSubPage({
    super.key,
    this.sectionID,
    this.sortType,
  });

  @override
  State<CartoonSectionSubPage> createState() => _CartoonSectionSubPageState();
}

class _CartoonSectionSubPageState extends State<CartoonSectionSubPage> {
  late CartoonSectionSubLogic logic;
  late String tag;

  @override
  void initState() {
    super.initState();
    tag = (widget.sortType ?? 0).toString();
    logic = Get.put(
      CartoonSectionSubLogic(
        sortType: widget.sortType,
        sectionID: widget.sectionID,
      ),
      tag: tag,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartoonSectionSubLogic>(
      tag: tag,
      builder: (_) {
        if (_.dataList == null) return LoadingCenterWidget();

        return pullYsRefresh(
          onInit: (ctr) => _.refreshCtr = ctr,
          onRefresh: (ctr) => _.loadData(),
          onLoading: (ctr) => _.loadMoreData(),
          child: () {
            if (_.dataList?.isEmpty == true) return CErrorWidget();
            return GridView.builder(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 12,
                crossAxisSpacing: 5,
                childAspectRatio: 111 / 190,
              ),
              itemCount: _.dataList?.length ?? 0,
              itemBuilder: (context, index) {
                CartoonMediaInfo? info = _.dataList?[index];
                return AcgItemWidget(info: info);
              },
            );
          }(),
        );
      },
    );
  }
}
